package cn.com.code.hwjs;

import java.util.*;

/**
 * https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682?tpId=37&tqId=21241&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
 *
 * @author zhengpei
 * @date 2022/5/8
 */
public class Hwjs18 {

  /**
   * 1、如果是0...* 或 127...* 跳过
   * 2、如果掩码错误，直接错误+1，扫描下一个
   * 3、如果掩码正确，判断ip，如果ip错误，错误+1
   * 4、如果ip正确，判断ip类型
   * 5、
   * 如果是A类型， A++ 如果是私人ip，privateIp++
   * 如果是B类型， B++ 如果是私人ip，privateIp++
   * 如果是C类型， C++ 如果是私人ip，privateIp++
   * 如果是D类型， D++
   * 如果是E类型， E++
   *
   * @param args
   */
  private static int A = 0;
  private static int B = 0;
  private static int C = 0;
  private static int D = 0;
  private static int E = 0;
  private static int errorIpOrMask = 0;
  private static int privateIp = 0;

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    List<String> ipMarks = new ArrayList<>();
    while (scanner.hasNextLine()) {
      String Line = scanner.nextLine();
      if (Line.equals("")) {
        break;
      }
      ipMarks.add(Line);
    }
    Map<String, String> ipAMarkC = new HashMap<>();

    for (String ipMask : ipMarks) {
      String[] split = ipMask.split("~");
      String ip = split[0];

      String[] ipNum = ip.split("\\.");
      String ipFirst = ipNum[0];
      if (null != ipFirst && !"".equals(ipFirst) && !"0".equals(ipFirst) && !"127".equals(ipFirst)) {
        List<String> strings = Arrays.asList(ipNum);
        if (strings.contains(".")) {
          errorIpOrMask++;
        } else {
          ipAMarkC.put(ip, split[1]);
        }
      }
    }

    ipAMarkC.forEach(
        (ip, markCode) -> {
          // 如果掩码校验失败，不校验ip
          if (markCodeValidate(markCode)) {
            ipHandler(ip);
          }
        }
    );
    System.out.println(A + " " + B + " " + C + " " + D + " " + E + " " + errorIpOrMask + " " + privateIp);

  }

  // 校验子网掩码这里最麻烦
  private static boolean markCodeValidate(String markCode) {
    String[] codes = markCode.split("\\.");
    // 补齐8位二进制
    String zero = "00000000";
    StringBuilder result = new StringBuilder();
    // 这里我用两个count来区分全为0和全为1，大家想想有没有更好的办法
    int zeroCount = 0;
    int twoCount = 0;
    for (String code : codes) {
      if (code.equals("0")) {
        zeroCount++;
      }
      if (code.equals("255")){
        twoCount++;
      }
      String bin = Integer.toBinaryString(Integer.parseInt(code));

      //字符串拼接捏
      result.append(zero.substring(0,8-bin.length()) + bin);
    }
    if (zeroCount == 4 || twoCount == 4){
      errorIpOrMask += 1;
      return false;
    }
    // 这里我通过切割判断数组长度，因为子网掩码一定的1和0左右分开的
    String[] split = result.toString().split("[0]");
    if (split.length <= 1){
      return true;
    }
    errorIpOrMask += 1;
    return false;
  }

  private static void ipHandler(String ip) {
    // 先截取第一个地址处理 abcde类地址
    // 再判断是否是私网ip
    String[] codes = ip.split("\\.");

    // 为地址分类
    Integer firstCode = Integer.valueOf(codes[0]);

    // 为地址分类
    Integer secondCode = Integer.valueOf(codes[1]);
    boolean canIsPrivate = false;
    if (firstCode >= 1 && firstCode <= 126){
      A += 1;
      canIsPrivate = true;
    }
    if (firstCode >= 128 && firstCode <= 191){
      B += 1;
      canIsPrivate = true;
    }
    if (firstCode >= 192 && firstCode <= 223){
      C += 1;
      canIsPrivate = true;
    }
    if (firstCode >= 224 && firstCode <= 239) {
      D += 1;
    }
    if (firstCode >= 240 && firstCode <= 255) {
      E += 1;
    }

    if (canIsPrivate){
      // 检查是否为私网地址
      if (firstCode.equals(10)  || (firstCode.equals(172) && (secondCode >= 16 && secondCode <= 31)) || (firstCode.equals(192) && secondCode.equals(168))){
        privateIp += 1;
      }
    }
  }
}
